package com.jwb.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jwb.mapper.dao.UserDao;
import com.jwb.mapper.model.User;
import com.jwb.pub.model.Jwb;
import com.jwb.pub.dao.BaseDAO;
import com.jwb.util.RSA;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Controller
@RequestMapping(value = "/test")
public class BootTestController {

    @Autowired
    private Jwb jwb;
    @Autowired
    private RedisTemplate redisTemplate;

    @Value("${name}")
    private String name;

    @Autowired
    private UserDao userDao;

    @Autowired
    private BaseDAO baseDAO;

    @RequestMapping("/hi")
    @ResponseBody
    public String hi(){
       Jwb jwb=new Jwb();
        return "h";
    }

    @RequestMapping(value = "/cachelist")
    @ResponseBody
    public JSONObject cachelist(HttpServletRequest request, HttpServletResponse response) {
        JSONObject jsobject = new JSONObject();
        try{
            JSONArray result = new JSONArray();
            Set<String> keys = redisTemplate.keys("*");
            for(String key:keys){
                JSONObject jso = new JSONObject();
                String type = redisTemplate.type(key).name();
                jso.put("key", key);
                if(type.toLowerCase().equals("string")){
                    Object obj = redisTemplate.opsForValue().get(key);
                    System.out.println(obj.getClass().getName());
                    jso.put("type", obj.getClass().getName());
                    if(obj instanceof JSONObject){
                        jso.put("value", ((JSONObject)obj).toString());
                    }else if(obj instanceof String){
                        jso.put("value", (String) obj);
                    }else{
                        jso.put("value", obj.getClass().getName());
                    }

                }else{
                    jso.put("value", type);
                }
                result.add(jso);
            }

            List<JSONObject> list = new ArrayList<JSONObject>();
            for(int i=0;i<result.size();i++){
                list.add(result.getJSONObject(i));
            }
            Collections.sort(list, new Comparator<JSONObject>(){
                @Override
                public int compare(JSONObject o1, JSONObject o2) {
                    String a = o1.getString("key");
                    String b = o2.getString("key");
                    return a.compareTo(b);
                }
            });
            result.clear();
            for(int j=0;j<list.size();j++){
                result.add(list.get(j));
            }

            jsobject.put("respCode", "0");
            jsobject.put("respDesc", "ok");
            jsobject.put("list", result);
        }catch(Exception e){
            e.printStackTrace();
            jsobject.put("respCode", "-1");
            jsobject.put("respDesc", "获取缓存失败");
        }
        return jsobject;
    }

    @RequestMapping(value = "/jwb")
    @ResponseBody
    public String jwb(){

        return jwb.toString();
    }


    @RequestMapping(value = "/name")
    @ResponseBody
    public String  name(){
        return name;
    }

    @RequestMapping(value = "/home")
    public String index(Model model){
        String org = "";
        model.addAttribute("title",name);
        model.addAttribute("message","");
        return "index";
    }



    /**
     * 静态资源 修改免重启配置
     * https://blog.csdn.net/jiazimo/article/details/106257904
     */
    @RequestMapping(value = "/cntuser")
    @ResponseBody
    public String cntUser() throws Exception{
        String sql = "SELECT COUNT(*) FROM user_info where sts='A' ";
        return baseDAO.findBySqlCnt(sql)+"";
//        return userDao.countUser()+"";
    }

    @RequestMapping(value = "/userinfo")
    @ResponseBody
    public User userInfo() throws Exception{
        User user = userDao.info("liuchang");
        return user;
    }

    @ResponseBody
    @RequestMapping(value = "/loginSuccess")
    public String loginSuccess(){
        return "登录成功";
    }

    @ResponseBody
    @RequestMapping(value = "genKey")
    public String genKey() throws Exception{
        return RSA.genKeyPair().toString();
    }



    // 数据库密码
    @ResponseBody
    @RequestMapping(value="/encode")
    public String encode(){
        return "";
    }


}
